package com.copm.ifm.servers.sys.service.impl.process;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.copm.ifm.base.api.pojo.PageDTO;
import com.copm.ifm.base.api.pojo.PageVO;
import com.copm.ifm.base.basic.ServiceResponse;
import com.copm.ifm.base.basic.enums.ResponseCodeEnum;
import com.copm.ifm.base.basic.util.AuthUtils;
import com.copm.ifm.base.service.mp.page.PageProxy;
import com.copm.ifm.servers.sys.dto.SysSupplierPageDTO;
import com.copm.ifm.servers.sys.dto.SysSupplierUpdateDTO;
import com.copm.ifm.servers.sys.entity.SysSupplier;
import com.copm.ifm.servers.sys.mapper.process.SysSupplierMapper;
import com.copm.ifm.servers.sys.service.process.SysSupplierService;
import com.copm.ifm.servers.sys.vo.SysSupplierPageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 供应商（推荐）表 服务实现类
 *
 * @author msp
 * @since 2020-12-30
 */
@Service
public class SysSupplierServiceImpl extends ServiceImpl<SysSupplierMapper, SysSupplier> implements SysSupplierService {

    private static final Logger log = LoggerFactory.getLogger(SysSupplierServiceImpl.class);

    @Override
    public PageVO<SysSupplierPageVO> selectForPage(PageDTO<SysSupplierPageDTO> pageDTO) {
        if ("name".equals(pageDTO.getSortName())) {
            pageDTO.setSortName("CONVERT( supplierName USING gbk )");
        }
        PageVO<SysSupplierPageVO> proxy = PageProxy.proxy(baseMapper::selectForPage, pageDTO);
        List<SysSupplierPageVO> records = proxy.getRecords();
        ServiceResponse<List<SysSupplierPageVO>> success = new ServiceResponse<>(records, ResponseCodeEnum.SUCCESS);
        proxy.setRecords(success.getData());
        return proxy;
    }

    @Override
    public void update(SysSupplierUpdateDTO updateDTO) {
        Long userId = AuthUtils.getCurrentUserId();
        log.info("修改供应商推荐信息，userId = " + userId);
        SysSupplier sysSupplier = new SysSupplier();
        sysSupplier.setId(updateDTO.getId());
        sysSupplier.setSeq(updateDTO.getSeq());
        sysSupplier.setStars(updateDTO.getStars());
        sysSupplier.setRecommended(updateDTO.getRecommended());
        sysSupplier.setUpdateBy(userId);
        baseMapper.updateById(sysSupplier);
    }
}
